package com.mobile.gridUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.mobile.tools.BeanUtil;


public class ParseFilterData {
	
	private static String regex="filter\\[([^\\]]+)\\]\\[([^\\]]+)\\]$";
	
	public static List<GridFilter> parse(Map<String,FilterBean> filterBeanMap){
		List<GridFilter> gridFilterList=new ArrayList<GridFilter>();
		for(Map.Entry<String,FilterBean> entry: filterBeanMap.entrySet()){
			FilterBean fb=entry.getValue();
			String type=fb.getType();
			GridFilter gridFilter=null;
			if("boolean".equals(type)){
				gridFilter=new BooleanFilter();
			}else if("string".equals(type)){
				gridFilter=new StringFilter();
			}else if("numeric".equals(type)){
				gridFilter=new NumericFilter();
			}else if("date".equals(type)){
				gridFilter=new DateFilter();
			}else if("list".equals(type)){
				gridFilter=new ListFilter();
			}
			BeanUtil.copyProperties(gridFilter,fb);
			gridFilterList.add(gridFilter);
		}
		return gridFilterList;
	}
	/**
	 * 解析gridfilter发送到后台的字符串
	 * @param parseObj		要解析的字符串
	 * @return	String[]	返回字符串数组
	 */
	public static String[] parse(String parseObj){
		Pattern p=Pattern.compile(regex);
		Matcher m=p.matcher(parseObj);
		String[] array=null;
		if(m.matches()){
			int num=m.groupCount();
			array=new String[2];
			for(int i=0;i<num;i++){
				array[i]=m.group(i+1);
			}
		}
		return array;
	}
}
